<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div v-for="(item,index) in roleList" :key="item.roleId" class="role-item" :class="{active: index === activeIndex}" @click="changeRole(index,item.roleId)">
        <div class="role-info">
          <svg-icon :icon-class="index === activeIndex ? 'user-active' : 'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <!-- 没有原声事件的组件，如果要使用原生事件需要使用.native 修饰符 -->
              <el-dropdown-item @click.native="$router.push(`/sys/role/addRole/${item.roleId}`)">编辑角色</el-dropdown-item>
              <el-dropdown-item @click.native="delRole(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/sys/role/addRole')">添加角色</el-button>
    </div>
    <!-- 树形 功能权限列表 -->
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="(item, index ) in permissionList" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree ref="tree" :data="item.children" node-key="id" show-checkbox default-expand-all :props="{ label: 'title' }" :default-checked-keys="perms[index]" />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员(${roleUserList.length})`" name="user">
          <el-table :data="roleUserList" style="width: 100%">
            <el-table-column type="index" width="250" label="序号" />
            <el-table-column prop="name" label="员工姓名" />
            <el-table-column prop="userName" label="登录账号" />
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { getRole, getPermission, getRolePermission, getRoleUser, deleteRole } from '@/api/Role'
export default {
  name: 'Role',
  data() {
    return {
      roleList: [], // 角色列表
      permissionList: [], // 功能权限列表
      activeIndex: 0, // 当前选中角色下标
      perms: [], // 当前角色权限
      activeName: 'tree', // 默认选中功能权限
      roleUserList: [] // 当前角色关联用户列表
    }
  },
  created() {
    this.getRole()
    this.getPermission()
    // 默认展示第一项权限数据
  },
  async mounted() {
    await this.getRole
    await this.getPermission()
    // 初始化时获取当前角色权限点
    this.changeRole(0, this.roleList[0].roleId)
  },
  methods: {
    // 封装获取当前角色关联用户列表
    async getRoleUser(roleId) {
      const res = await getRoleUser(roleId)
      // console.log(res)
      this.roleUserList = res.data.rows
    },
    // 封装获取当前角色权限
    async getRolePermission(roleId) {
      const res = await getRolePermission(roleId)
      this.perms = res.data.perms
    },
    // 通过递归禁用所有子节点
    addDisabled(roleList) {
      roleList.forEach(item => {
        item.disabled = true
        if (item.children) {
          this.addDisabled(item.children)
        }
      })
    },
    // 获取角色列表
    async getRole() {
      const res = await getRole()
      // console.log(res)
      this.roleList = res.data
      // console.log(this.roleList)
    },
    // 切换角色 通过角色id获取点击角色权限
    async changeRole(index, id) {
      this.activeIndex = index
      // 获取当前角色权限
      await this.getRolePermission(id)
      // 调用 setCheckedKeys 方法
      const treeList = this.$refs.tree
      treeList.forEach((tree, index) => {
        tree.setCheckedKeys(this.perms[index])
      })
      // 获取当前角色关联用户列表
      await this.getRoleUser(id)
    },
    // 查询所有功能权限
    async getPermission() {
      const res = await getPermission()
      // console.log(res)
      this.permissionList = res.data
      // 禁用所有子节点
      this.addDisabled(this.permissionList)
    },
    // 删除角色
    delRole(id) {
      this.$confirm('是否确认删除当前角色?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.getRole()
        this.$message.success('删除成功')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
